from typing import *

class MessageHandler:
    def __init__(self):
        self.handlers = {
            '查询盲盒': self._handle_query_personal_manghe,
            '查询房间盲盒': self._handle_query_room_manghe,
            'error': self._handle_error
        }
    
    def callback(self, message_type, *args, **kwargs):
        """对外提供的统一回调接口"""
        handler = self.handlers.get(message_type, self._handle_unknown)
        return handler(*args, **kwargs)
    
    def _calculate_manghe_stats(self, gift_list: List[Dict]) -> tuple:
        """计算盲盒基础统计数据"""
        white = black = 0
        real = original = 0
        
        for item in gift_list:
            count = item['num']
            real += item['real_coin'] * count
            original += item['original_coin'] * count
            white += count if item['original_coin'] < item['real_coin'] else 0
            black += count if item['original_coin'] >= item['real_coin'] else 0
            
        return white, black, real, original

    def _generate_result_string(self, total: int, white: int, black: int, 
                            real: int, original: int, prefix: str = "") -> str:
        """生成结果字符串"""
        profit = real - original
        status = "赚" if profit >= 0 else "送"
        return f"{prefix}共赠送盲盒{total}个，价值{int(real*10)}电池，{status}叔叔{int(abs(profit)*10)}电池，{white}白_{black}黑"

    def _handle_query_personal_manghe(self, usr_name: str, gift_map: Dict[str, List]) -> List[str]:
        """处理个人盲盒查询"""
        if usr_name not in gift_map:
            return ["用户无盲盒记录"]
            
        white, black, real, original = self._calculate_manghe_stats(gift_map[usr_name])
        return self._optimized_split_string(
            self._generate_result_string(white+black, white, black, real, original, '今天你')
        )

    def _handle_query_room_manghe(self, gift_map: Dict[str, List]) -> List[str]:
        """处理房间盲盒查询"""
        white = black = real = original = 0
        for gifts in gift_map.values():
            w, b, r, o = self._calculate_manghe_stats(gifts)
            white += w
            black += b
            real += r
            original += o
            
        return self._optimized_split_string(
            self._generate_result_string(white+black, white, black, real, original, "今天本房间")
        )
    
    def _handle_error(self, error_msg):
        """处理错误消息"""
        return f"Error occurred: {error_msg}"
    
    def _handle_unknown(self, *args):
        """处理未知消息类型"""
        return "Unknown message type"
    
    def _optimized_split_string(self, text: str, max_len: int = 20) -> list[str]:
        if not isinstance(text, str) or max_len <= 0:
            return []
        return [text[i:i+max_len] for i in range(0, len(text), max_len)]

# 使用示例
if __name__ == "__main__":
    handler = MessageHandler()
    test = {}
    print(handler.callback('查询房间盲盒', test))  # 输出: Hello, Alice!
    # print(handler._optimized_split_string(test))